package com.lst.utils;

import java.io.InputStream;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lst.common.CommCode;
import com.lst.common.JPushEnum;
import com.lst.common.JPushMessage;
import com.lst.model.MstMessage;
import com.lst.model.Plan;
import com.lst.model.PlanCourse;
import com.lst.service.MstMessageService;
import com.lst.service.PlanCourseService;
import com.lst.service.PlanService;

public class TimerTaskTest extends java.util.TimerTask{ 
     
	  private static boolean isRunning = false;  
	  private ServletContext context = null;   
	  protected static ClassPathXmlApplicationContext ctx;
	  protected static String path;
	  
      private PlanCourseService planCourseService = ctx.getBean(PlanCourseService.class);
	  
	  private PlanService planService = ctx.getBean(PlanService.class);
	  
	  private MstMessageService mstMessageService = ctx.getBean(MstMessageService.class);

		/**
		 * @return the path
		 */
		public static String getPath() {
			return path;
		}

		/**
		 * @param path
		 *            the path to set
		 */
		public static void setPath(String path) {
			TimerTaskTest.path = path;
		}

	public TimerTaskTest(ServletContext context)  
	  {  
	      this.context = context;   
	  }  
	
	static {
		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

		try {
			Properties props = new Properties();

			ClassLoader cl = CommonUtils.class.getClassLoader();

			InputStream is = cl.getResourceAsStream("kungfu.properties");

			props.load(is);

			path = props.getProperty("img_file_path_linux");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	
	
	
	  
	  public void run()  
	  {  
	    Calendar c = Calendar.getInstance();  
	    if(!isRunning){  
	    	
	        Date startDate = new Date();
	        try {
				Date trainDate=DateUtil.getFormatDate(startDate, DateUtil.ALL_DATE_HORIZONTAL);//日期转换成yyyy-MM-dd
		        Map<String,Object> map=new HashMap<String, Object>();
				map.put("isDelete", 0);
				map.put("planStatus", 1);
				map.put("trainDate", trainDate);
		        //在这里编写自己的功能
		        List<PlanCourse> planCourseList=planCourseService.selectByMap2(map);
		        if(planCourseList!=null){
		        	SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		        	
		        	for(PlanCourse planCourse:planCourseList){
		        		Plan plan=planService.selectById(planCourse.getPlanId());
		        		if(plan!=null){
		        			
		        			//训练天数为10天，连续3个训练日没练，计划过期； 训练天数为14天，连续4个训练日没练，计划过期； 训练天数为18天，连续5个训练日没练，计划过期； 训练天数为22天，连续6个训练日没练，计划过期；
		          		  Map<String,Object> countMap=new HashMap<String,Object>();
		          		  countMap.put("planId", plan.getId());
		          		  countMap.put("trainDate", startDate);
		          		  int countNum=planCourseService.countByMap(countMap);
		          		  int num1=plan.getFrequency();
		          		  int num2=0;
		          		  if(num1==10){
		          			  num2=3;
		          		  }
		          		 if(num1==14){
		         			  num2=4;
		         		  }
		          		 if(num1==18){
		         			  num2=5;
		         		  }
		          		 if(num1==22){
		         			  num2=6;
		         		  }
		          		//  System.out.println("没训练日："+countNum);
		          		  if(countNum!=0&&countNum>=num2){
		          			// System.out.println("计划过期"+plan.getCreateUser());//过期
		          		  }else if(countNum==0){
		          			 int countNum2=planCourseService.countByMap2( countMap);
		          			// System.out.println("之前总的没训练日："+countNum2);
		          			 if(countNum2>=num2){
		          				// System.out.println("计划过期"+plan.getCreateUser());//过期
		          			 }else{
		          				
		          				if(plan.getIsNotice()){
				        			String noticeTime=plan.getNoticeTime();
				        			Time btime=new Time(sdf.parse(noticeTime).getTime());
				        			int C_SCHEDULE_HOUR=btime.getHours();
				        			int C_SCHEDULE_MINUTE=btime.getMinutes();
				        		    if(C_SCHEDULE_HOUR == c.get(Calendar.HOUR_OF_DAY) && C_SCHEDULE_MINUTE==c.get(Calendar.MINUTE)){  
				        		        isRunning = true;  
				        		 //      context.log("开始执行指定任务");
				        		        
									      MstMessage message=new MstMessage();
									      message.setReceiveid(plan.getCreateUser());
									      message.setReceivetype(CommCode.SYSCODE_MSG_TYPE_RECEIVE_CLIENT);
									      message.setSendid(1);
									      message.setSendtype(CommCode.SYSCODE_MSG_TYPE_SEND_SYSTEM);
									      message.setContent("今天有个训练计划，别忘了去训练哦，亲！");
									      message.setEnabled(true);
									      message.setIsdeleted(false);
									      message.setCreateuser(1);
									      message.setCreatedate(startDate);
									      message.setReserved2(1);
									      message.setReserved7(false);
									      message.setType(CommCode.SYSCODE_MSG_TYPE_PUSH);
									      mstMessageService.insert(message);
									      JPushMessage.pushMessage2(message, JPushEnum.ALL_ALIAS);
				        		
				        		        //-------------------结束  
				        		        isRunning = false;  
				        		 //       context.log("指定任务执行结束！");  
				        		      }else{  
				        		 //       context.log("没有执行任务！");  
				        		      }  
				        		}
		          				 
		          			 }
		          		  }else{
		          			if(plan.getIsNotice()){
			        			String noticeTime=plan.getNoticeTime();
			        			Time btime=new Time(sdf.parse(noticeTime).getTime());
			        			int C_SCHEDULE_HOUR=btime.getHours();
			        			int C_SCHEDULE_MINUTE=btime.getMinutes();
			        		    if(C_SCHEDULE_HOUR == c.get(Calendar.HOUR_OF_DAY) && C_SCHEDULE_MINUTE==c.get(Calendar.MINUTE)){  
			        		        isRunning = true;  
			        		 //      context.log("开始执行指定任务");
								      MstMessage message=new MstMessage();
								      message.setReceiveid(plan.getCreateUser());
								      message.setReceivetype(CommCode.SYSCODE_MSG_TYPE_RECEIVE_CLIENT);
								      message.setSendid(1);
								      message.setSendtype(CommCode.SYSCODE_MSG_TYPE_SEND_SYSTEM);
								      message.setContent("今天有个训练计划，别忘了去训练哦，亲！");
								      message.setEnabled(true);
								      message.setIsdeleted(false);
								      message.setCreateuser(1);
								      message.setCreatedate(startDate);
								      message.setReserved2(1);
								      message.setReserved7(false);
								      message.setType(CommCode.SYSCODE_MSG_TYPE_PUSH);
								      mstMessageService.insert(message);
								      JPushMessage.pushMessage2(message, JPushEnum.ALL_ALIAS);
			        		
			        		        //-------------------结束  
			        		        isRunning = false;  
			        		 //       context.log("指定任务执行结束！");  
			        		      }else{  
			        		 //       context.log("没有执行任务！");  
			        		      }  
			        		}
		          			  
		          		  }
		        			
		        		}
		        	}
		        }
		        
			} catch (ParseException e) {
				e.printStackTrace();
			}
	    	
	    }  
	  }  
} 
